package com.xzlcPT.controller;

import com.util.PageBean;
import com.xzlcPT.bean.XzUser;
import com.xzlcPT.service.XzUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/1.
 */
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private XzUserService userService;

    @ResponseBody
    @RequestMapping("selUserAll")
    public Map selUserAll(@RequestParam(defaultValue="1")int page, @RequestParam(defaultValue="5")int rows, XzUser user){
        Map map = new HashMap();
        PageBean<XzUser> pageBean = userService.selUserAll(page,rows,user);
        map.put("userList",pageBean.getList());
        map.put("page",pageBean.getPageNum());
        map.put("pages",pageBean.getPages());
        map.put("total",pageBean.getTotal());
        map.put("rows",pageBean.getPageSize());
        return map;
    }

    @ResponseBody
    @RequestMapping("selUserById")
    public Map selUserById(Long id){
        System.out.println(id);
        Map map = new HashMap();
        XzUser user = userService.selUserById(id);
        map.put("user",user);
        return  map;
    }

    @ResponseBody
    @RequestMapping("updateUser")
    public Map updateUser(XzUser user){
        Map map = new HashMap();
        int i = userService.updateUser(user);
        if(i>0){
            map.put("msg","ok");
        }else{
            map.put("msg","err");
        }
        return map;
    }
    @ResponseBody
    @RequestMapping("insertUser")
    public Map insertUser(XzUser user){
        Map map = new HashMap();
        int i = userService.insertUser(user);
        if(i>0){
            map.put("msg","ok");
        }else{
            map.put("msg","err");
        }
        return map;
    }
    @ResponseBody
    @RequestMapping("deleteUser")
    public Map deleteUser(Long id){
        Map map = new HashMap();
        int i = userService.deleteUser(id);
        if(i>0){
            map.put("msg","ok");
        }else{
            map.put("msg","err");
        }
        return map;
    }
}
